// EditDialog.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.Material
import "../components"

Dialog {
    id: dialog
    title: "编辑学生信息"
    modal: true
    standardButtons: Dialog.Save | Dialog.Cancel
    Material.theme: Material.Light
    Material.accent: Material.primary

    x: (parent.width - width) / 2
    y: (parent.height - height) / 2
    width: Math.min(600, parent.width * 0.8)

    property var studentData

    function open(student) {
        studentData = student
        studentForm.loadData(student)
        dialog.open()
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 10

        StudentForm {
            id: studentForm
            Layout.fillWidth: true

            function loadData(student) {
                idField.text = student.id
                nameField.text = student.name
                collegeField.text = student.college
                deptField.text = student.department
                classField.text = student.className
                dormField.text = student.dormitory
                hometownField.text = student.hometown
                phoneField.text = student.phone

                if (student.gender === "女") {
                    femaleRadio.checked = true
                } else {
                    maleRadio.checked = true
                }
            }
        }
    }

    onAccepted: {
        StudentService.updateStudent(
            studentData.id,
            studentForm.getStudentData()
        )
    }
}